package com.testdemo;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.lang.reflect.Field;

public class demo {
    public static void main(String[] args) throws Exception {
        Student s = new Student("zhangsan", 3);
        Teacher t = new Teacher("laoliu","哈哈");
        savaObject(s);
    }

    private static void savaObject(Object o) throws Exception {
        Class clazz = o.getClass();

        //创建IO流
        BufferedWriter writer = new BufferedWriter(new FileWriter("F:\\重头来过\\代码\\stu_reflect\\a.txt"));
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            Object o1 = field.get(o);
            //写出数据
            writer.write(name + "=" + o1);
            writer.newLine();
        }
        writer.close();

    }
}
